<template>
  <div>
    <div style="display:flex;justify-content: center;">
      <div>
        <h1 style="letter-spacing:0.1em;text-align: center">{{ this.taskdata.assessName }}</h1>
        <el-row>
          <el-col class="basicCondition" style="width: 1000px;">
            <el-row>
              <el-col>
                <div>
                  <h3 class="h2Bold">一、基本情况</h3>
                </div>
              </el-col>
            </el-row>
            <el-row type="flex" justify="space-between">
              <el-col :span="6">姓名: {{ patientName }}</el-col>
              <el-col :span="6">性别: {{ gender }}</el-col>
              <el-col :span="6">年龄: {{ patientAge }}</el-col>
              <el-col :span="6">科室: {{ pdepartment }}</el-col>
            </el-row>
            <el-row>患者编号：{{ `000${patientNo}` }}</el-row>
            <el-row style="letter-spacing:0.1em;line-height: 1.5em;">病情摘要：{{ illnessSummary }}</el-row>
            <el-row style="letter-spacing:0.1em;line-height: 1.5em;">既往史：{{ preHistory }}</el-row>
            <el-row style="letter-spacing:0.1em;line-height: 1.5em;">诊断：{{ preDiagnose }}</el-row>
            <el-row>
              <el-col :span="6">
                评定日期：{{ assessTime }}
              </el-col>
              <el-col :span="6">
                评定人：{{ assessDoctor }}
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-form ref="pgForm" label-width="80px">
<!--          <div v-for="assname in comList" :assname="assname" style="margin:10px 0">
            <keep-alive>
              <component
                :is="assname"
                ref="com"
              ></component>
            </keep-alive>
          </div>-->
          <div v-for="(item,index) in comList">
            <el-row type="flex" align="middle" style="margin-top: 10px">
              <el-col>
                <div class="grid-content">
                  <h3 class="h2Bold">{{ item.titleName }}</h3>
                </div>
              </el-col>
            </el-row>
            <div class="containerClass">
              <el-row v-for="assname in item.assConfig" :assname="assname">
                <el-col :span="22" :offset="1">
                  <keep-alive>
                    <component
                      :is="assname"
                      ref="com"
                    ></component>
                  </keep-alive>
                </el-col>
              </el-row>
            </div>
          </div>
          <div style="text-align: center;margin: 30px 0">
            <p style="display: inline-block;margin-right: 20px;font-size: 16px">选择评定阶段：</p>
            <template>
              <el-radio-group v-model="phase">
                <el-radio :label="1">初期</el-radio>
                <el-radio :label="2">中期</el-radio>
                <el-radio :label="3">末期</el-radio>
              </el-radio-group>
            </template>
          </div>
          <div v-show="status==5" style="margin: 30px 0;">
            <h3>审核意见:</h3>
            <textarea style="width:90%;padding:20px 20px;font-size: 22px;border: 2px;border-radius: 0.5rem;background-color: #eeeeee">
              {{auditOpinion}}
            </textarea>
          </div>
          <div style="text-align: center">
            <el-button type="warning" @click="save">暂存</el-button>
            <el-button type="primary" @click="submitForm">提 交</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-form>


      </div>
    </div>
  </div>
</template>

<script>

import {getAssess} from "@/api/gaoxin/assess";
import {addData, getData, listData, updateData} from "@/api/gaoxin/data";
import {formatDate} from "@/utils";
import {listPatient} from "@/api/gaoxin/patient";
import path from "path";
import {getText, updateTask} from "@/api/gaoxin/taskdata";
import Recorder from 'js-audio-recorder'
import {listClinic} from "@/api/gaoxin/clinic";

const lamejs = require('lamejs')
export default {
  name: "dataAdd",
  components: {},
  data() {
    return {
      auditOpinion:'',
      status:'',
      //任务数据
      taskdata: {},
      phase:'',
      //患者基本、就诊信息
      // dataList: {},
      patientBasicInfo: {},
      patientName: '',
      patientGender: '',
      patientAge: 0,
      patientNo: '',
      patientClinicInfo: {},
      department: '',
      illnessSummary: '',
      preHistory: '',
      preDiagnose: '',
      assessDoctor: '',
      //组件列表
      comList: [],
      // 评估表数据
      assess: {},
      fromdata: [],
      //评估数据
      pgForm: "",
      // 查询参数
      queryParams: {
        pageSize: 10,
        patientName: null,
        masterName: null,
        doctorName: null,
        assessName: null,
        phase: null,
        status: null,
        createTime: null,
        clinicId: null,
      },
      //基本信息查询参数
      queryBasicParams: {
        patientId: null
      },
      total: '',
      title: '',
      open: false,
      loading: false,
      auditOnOff: '',
    }
  },
  created() {
    this.getConfigKey("gx.audit.auditOnOff").then(response => {
      this.auditOnOff = response.msg;
      // console.log("审核通过"+this.auditOnOff);
    });
    this.taskdata = this.$route.params.taskdata;
    this.queryBasicParams.patientId = this.taskdata.patientId;
    this.assessDoctor = this.taskdata.doctorName;
    this.phase=this.taskdata.phase
    this.status=this.taskdata.status
    this.getBasicData();
    this.getClinicData();
    var assessId = this.$route.params.taskdata.assessId;
    getAssess(assessId).then(res => {
      this.assess = res.data;
      this.comList = JSON.parse(this.assess.assessConfig);
    })

    if (this.taskdata.status == 4 || this.taskdata.status == 5) {
      listData({taskId: this.taskdata.taskId}).then(response => {
        const taskData = response.rows;
        this.fromdata[0] = taskData[0];
        this.auditOpinion=this.fromdata[0].auditOpinion
        var temp1=this.fromdata[0].assessData;
        // console.log(typeof temp1);
        // console.log('temp1',temp1);
        // var reg=/\\/g;
        // let replaceAfter = temp1.toString().replace(reg,'');
        let replaceAfter = temp1.toString();
        // console.log('replaceAfter',replaceAfter);
        this.fromdata[0].assessData=JSON.parse(replaceAfter);
        // console.log('this.fromdata[0].assessData：',this.fromdata[0].assessData);
        // console.log('this.comList：',this.comList);
        let k = 0;
        for (let i = 0; i < this.comList.length; i++) {
          this.comList[i].assConfig.forEach(item => {
            let assname = item;
            this.$refs.com[k++].pgForm = this.fromdata[0].assessData[assname];
          });
        }
      });
    }
    // this.taskdata = this.$route.params.taskdata
    // console.log('taskdata:',this.taskdata)
    // // this.queryParams.clinicId = this.taskdata.clinicId
    // // this.getDataList()
    // //组件列表加载
    // var assessid = this.$route.params.assid;
    // getAssess(assessid).then(res => {
    //   this.assess = res.data;
    //   this.comList = this.assess.assessConfig.split(',')
    //   // this.datadeel()
    // })
  },
  computed: {
    pdepartment(){
      if(this.department === '0') return '手外科'
      if(this.department === '1') return '骨科'
    },
    gender(){
      if(this.patientGender === '0') return '男'
      if(this.patientGender === '1') return '女'
      if(this.patientGender === '2') return '未知'
    },
    assessTime(){
      const timeOne = new Date()
      const year = timeOne.getFullYear()
      let month = timeOne.getMonth() + 1
      let day = timeOne.getDate()
      month = month < 10 ? '0' + month : month
      day = day < 10 ? '0' + day : day
      const NOW_MONTHS_AGO = `${year}-${month}-${day}`
      return NOW_MONTHS_AGO
    }
  },
  methods: {
    // getDataList(){
    //   this.loading = true;
    //   getData(this.queryParams).then(response => {
    //     this.dataList = response.rows;
    //     console.log('dataList:',this.dataList)
    //     this.total = response.total;
    //     this.loading = false;
    //   });
    // },
    //查询患者基本信息
    getBasicData(){
      this.loading = true;
      listPatient(this.queryBasicParams).then(response => {
        this.patientBasicInfo = response.rows;
        this.patientName = this.patientBasicInfo[0].patientName;
        this.patientGender = this.patientBasicInfo[0].patientGender;
        this.patientAge = this.patientBasicInfo[0].patientAge;
        this.patientNo = this.patientBasicInfo[0].patientNo;
        this.loading = false;
      });
    },
    //查询患者就诊信息
    getClinicData(){
      listClinic(this.queryBasicParams).then(response => {
        this.patientClinicInfo = response.rows;
        const last = this.patientClinicInfo.length - 1;
        this.department = this.patientClinicInfo[last].department
        this.preDiagnose = this.patientClinicInfo[last].preDiagnose
        this.illnessSummary = this.patientClinicInfo[last].illnessSummary
        this.preHistory = this.patientClinicInfo[last].preHistory
      });
    },
    //数据处理-未使用
    datadeel() {
      var list = this.comList;
      for (const item of list) {
        this.pgForm[item] = {}
      }
    },
    //取消
    cancel() {
      this.$tab.closePage()
      this.$router.go(-1);
    },
    //取消弹窗
    cancelDialog() {
      this.sen = '';
      this.open = false;
      this.open1 = false;
    },
    /** 查询任务列表 */
    getList(taskId) {
      this.loading = true;
      this.queryParams.taskId = taskId;
      listData(this.queryParams).then(response => {
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 暂存按钮 **/
    save() {
      var pgform = {};
      let temp = 0;
      this.comList.forEach(item => {
        for (const x in item.assConfig) {
          var t = this.$refs.com[temp++].pgForm;
          pgform[item.assConfig[x]] = t;
        }
      });
      this.pgForm = JSON.stringify(pgform);
      const taskId = this.taskdata.taskId;

      if (this.taskdata.status == 1) {
        addData({
          assessData: this.pgForm,
          taskId: this.taskdata.taskId,
          phase: this.phase,
          status: 4,
        }).then(response => {
          this.$modal.msgSuccess("暂存成功");
          updateTask({taskId:taskId,status: 4,phase:this.phase})
          this.$tab.closeOpenPage({path: "/taskdata"})
        });
      }
      else {
        this.fromdata[0].assessData = this.pgForm;
        this.fromdata[0].phase = this.phase;
        let form = {};
        form = this.fromdata[0]
        form.status=4
        updateData(form).then(response => {
        }).then(response => {
          this.$modal.msgSuccess("暂存成功");
          updateTask({taskId:taskId,status: 4,phase:this.phase})
          this.$tab.closeOpenPage({path: "/taskdata"})
        });
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      var pgform = {};
      let temp = 0;
      // console.log('this.$refs.com:',this.$refs.com);
      /*for (const temp in this.comList) {
        var t = this.$refs.com[temp].pgForm
        console.log(t)
        pgform[this.comList[temp]] = t
      }*/
      this.comList.forEach(item => {
        for (const x in item.assConfig) {
          var t = this.$refs.com[temp++].pgForm;
          pgform[item.assConfig[x]] = t;
        }
      });
      this.pgForm = JSON.stringify(pgform);
      // console.log('this.pgForm:',this.pgForm);
      const taskId = this.taskdata.taskId;

      if (this.auditOnOff == 'false') {
        if (this.taskdata.status == 1) {
          addData({
            assessData: this.pgForm,
            taskId: this.taskdata.taskId,
            phase: this.phase,
            createTime: formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"),
            status: 3
          }).then(response => {
            this.$modal.msgSuccess("提交成功");
            updateTask({taskId:taskId,status: 3,phase:this.phase})
            this.$tab.closeOpenPage({path: "/taskdata"})
          });
        }
        else {
          this.fromdata[0].assessData = this.pgForm;
          this.fromdata[0].status = 2;
          this.fromdata[0].phase = this.phase;
          this.fromdata[0].createTime = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
          let form = {};
          form = this.fromdata[0];
          updateData(form).then(response => {
          }).then(response => {
            this.$modal.msgSuccess("提交成功");
            updateTask({taskId:taskId,status: 3,phase:this.phase})
            this.$tab.closeOpenPage({path: "/taskdata"})
          });
        }
      } else {
        if (this.taskdata.status == 1) {
          addData({
            assessData: this.pgForm,
            taskId: this.taskdata.taskId,
            phase: this.phase,
            createTime: formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"),
            status: 2
          }).then(response => {
            this.$modal.msgSuccess("提交成功");
            updateTask({taskId:taskId,status: 2,phase:this.phase})
            this.$tab.closeOpenPage({path: "/taskdata"})
          });
        }
        else {
          this.fromdata[0].assessData = this.pgForm;
          this.fromdata[0].status = 2;
          this.fromdata[0].phase = this.phase;
          this.fromdata[0].createTime = formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
          let form = {};
          form = this.fromdata[0];
          updateData(form).then(response => {
          }).then(response => {
            this.$modal.msgSuccess("提交成功");
            updateTask({taskId:taskId,status: 2,phase:this.phase})
            this.$tab.closeOpenPage({path: "/taskdata"})
          });
        }
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {
        });
    },

  },


}
</script>

<style scoped>
.tools{
  /*background-color: rebeccapurple;*/
  /*width: 100px;*/
  /*height: 100px;*/
  position:fixed;
  right: 10px;
  bottom:200px;
  z-index: 999;
}
.h2Bold {
  font-weight: 600;
}
.basicCondition .el-row{
  margin-bottom: 20px;
}
.containerClass {
  min-height: 80px;
  padding: 20px 0;
}
</style>
